package com.connection.second.web.controller.system;

import com.connection.second.common.annotation.Anonymous;
import com.connection.second.common.core.domain.entity.SysUser;
import com.connection.second.common.utils.SecurityUtils;
import com.connection.second.system.domain.SysUserRole;
import com.connection.second.system.mapper.SysUserRoleMapper;
import com.connection.second.system.service.ISysRoleService;
import com.connection.second.system.service.ISysUserService;
import com.connection.second.wallpaper.domain.dto.UserRegisterDto;
import com.connection.second.wallpaper.domain.entity.PaperUserInfo;
import com.connection.second.wallpaper.service.IPaperUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.connection.second.common.core.controller.BaseController;
import com.connection.second.common.core.domain.AjaxResult;
import com.connection.second.common.core.domain.model.RegisterBody;
import com.connection.second.common.utils.StringUtils;
import com.connection.second.framework.web.service.SysRegisterService;
import com.connection.second.system.service.ISysConfigService;

import java.util.ArrayList;
import java.util.List;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IPaperUserInfoService userInfoService;



    /**
     * 管理员注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 用户注册
     * @param registerBody
     * @return
     */
    @Anonymous
    @PostMapping("/register/user")
    @Transactional
    public AjaxResult registerUser(@Validated @RequestBody UserRegisterDto registerBody)
    {
        // 验证码开关
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        if (captchaEnabled)
        {
            registerService.validateCaptcha(registerBody.getUserName(), registerBody.getCode(), registerBody.getUuid());
        }
        SysUser user = new SysUser();
        BeanUtils.copyProperties(registerBody, user);

        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setRemark("普通用户");
        userService.insertUser(user);
        //        设置权限范围
        List<SysUserRole> list = new ArrayList<>();
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(user.getUserId());
        userRole.setRoleId(100L);
        list.add(userRole);
        userRoleMapper.batchUserRole(list);
//        设置用户信息
        PaperUserInfo paperUserInfo = new PaperUserInfo();
        paperUserInfo.setAccountId(user.getUserId());
        paperUserInfo.setNickname(user.getNickName());
        userInfoService.insertPaperUserInfo(paperUserInfo);
        return success();
    }
}
